interface IFormat {
  YYYY: string;
  MM: string;
  DD: string;
  HH: string;
  mm: string;
  ss: string;
  [index: string]: string;
}

export function dateFormat(val: string, format = "YYYY-MM-DD HH:mm:ss") {
  let date = new Date(val);
  if (!val) {
    return "";
  }
  const incZero = (val: number): string => {
    return val < 10 ? "0" + val : val + "";
  };

  const config: IFormat = {
    YYYY: incZero(date.getFullYear()),
    MM: incZero(date.getMonth()),
    DD: incZero(date.getDate()),
    HH: incZero(date.getHours()),
    mm: incZero(date.getMinutes()),
    ss: incZero(date.getSeconds()),
  };
  for (const key in config) {
    format = format.replace(key, config[key]);
  }
  return format;
}
// console.log(dateFormat(date, "YYYY年MM月DD日"));

// export function formateDate(val: string | null) {
//   if (!val) return "";
//   let date = new Date(val);
//   let year = date.getFullYear();
//   year < 10 ? "0" + year : year;
//   let month = date.getMonth();
//   month < 10 ? "0" + month : month;
//   let day = date.getDate();
//   day < 10 ? "0" + day : day;
//   let h = date.getHours();
//   h < 10 ? "0" + h : h;
//   let m = date.getMinutes();
//   m < 10 ? "0" + m : m;
//   let s = date.getSeconds();
//   s < 10 ? "0" + s : s;
//   return year + month + day + " " + h + m + s;
// }
